import Native from '@/service/native'
import asyncRouter from './async'

let backbool, timer

asyncRouter.registerRouteEvent('beforeEach', (to, from, next) => {
  backbool = false
  next()
})

const appBack = () => {
  backbool = true

  timer && window.clearTimeout(timer)
  timer = window.setTimeout(() => {
    // 超过限制时间没有触发 beforeEach 事件, 则说明退无可退
    if (backbool) Native.api.exit()
  }, 200)

  const router = asyncRouter.router
  router && router.back()
}

appBack.install = Vue => Vue.prototype.$_back = appBack

export default appBack
